﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Model;
using SDT.Model.Properties;
using SDT.Lib.Common;
using SDT.Lib.Paging;
using SDT.Lib.Helpers;
namespace SDT.Admin.Controllers
{ 
	[RoleHelper(Roles = RoleSystem.Dictionary)]
    public class CityController : Controller
    {
        /// <summary>
        /// Created by Vietlv
        /// Date 17/06/2011
        /// Description: Manage City - Class City controler;
        /// </summary>
        /// <returns></returns>
        
        SDTDataContext eDataConttext = new SDTDataContext();
        
        #region GET: /City/
        public ActionResult Index(int ? page)
        {
            try
            {
                    if (page == null) page = 1;
                    string order = UserState.getUserStateFromRequest("City.orderColum", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("City.orderDirection","orderDir","desc").ToString();
                    string keyword = UserState.getUserStateFromRequest("City.Keyword", "Keyword", "").ToString();
                    int pagesize = Convert.ToInt32(UserState.getUserStateFromRequest("City.PageSize","PageSize",Settings.Default.PageSize));
                    var listCity = eDataConttext.Cities
                        .Where(e => keyword.Trim().Length >0 ? e.Name.Contains(keyword.ToLower().Trim()) ||
                                e.Code.ToLower().Contains(keyword.ToLower().Trim()) ||
                                e.ShortName.ToLower().Contains(keyword.ToLower().Trim()) : true)
                        .OrderBy(string.Format("{0} {1}", order, orderDir))
                        .ToPagedList(page.Value - 1, pagesize);
                    ViewData["order"] = order;
                    ViewData["orderDir"] = orderDir;
                    return View(new CityViewModel { 
                        Cities = listCity,
                        Keyword = keyword
                    });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region GET: /City/Details/5

        public ActionResult Details(Byte id)
        {
                var itemCity = eDataConttext.Cities.Single(e => e.ID == id);
                return View(itemCity);
        }
        #endregion

        #region GET: /City/Create

        public ActionResult Create()
        {
                return View();
        } 
     #endregion

        #region POST: /City/Create

        [HttpPost]
        public ActionResult Create(City fromdata)
        {
                try
                {
                    City item = new City();

                    item.ID = Convert.ToByte(eDataConttext.Cities.Count() > 0 ? (eDataConttext.Cities.Max(e => (e.ID)) + 1) : 1);
                    item.Code = fromdata.Code;
                    item.Name = fromdata.Name;
                    item.ShortName = fromdata.ShortName;

                    eDataConttext.Cities.InsertOnSubmit(item);
                    eDataConttext.SubmitChanges();

                    return RedirectToAction("Index");
                }
                catch(Exception exp)
                { 
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
        }
        #endregion

        #region GET: /City/Delete/5

        public ActionResult Delete(Byte id)
        {
                try
                {
                    var item = eDataConttext.Cities.Single(e => e.ID == id);

                    return View(item);
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
        }
        #endregion

        #region POST: /City/Delete/5

        [HttpPost]
        public ActionResult Delete(Byte id, FormCollection collection)
        {
                try
                {
                    var item = eDataConttext.Cities.Single(e => e.ID == id);
                    eDataConttext.Cities.DeleteOnSubmit(item);
                    eDataConttext.SubmitChanges();
                    return RedirectToAction("Index");
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;    
                    return View();
                }
        }
        #endregion

        #region GET: /City/Edit/5

        public ActionResult Edit(Byte id)
        {
                try
                {
                    var item = eDataConttext.Cities.Single(e => e.ID == id);

                    return View(item);
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
        }
        #endregion

        #region POST: /City/Edit/5

        [HttpPost]
        public ActionResult Edit(Byte id, City fromdata)
        {
                try
                {
                    var currentItem = eDataConttext.Cities.Single(e =>e.ID == id);
                    currentItem.Code = fromdata.Code;
                    currentItem.Name = fromdata.Name;
                    currentItem.ShortName = fromdata.ShortName;
                    eDataConttext.SubmitChanges();
                    return RedirectToAction("Index");
                }
                catch (Exception exp)
                {
                    ViewData[Messages.MessageInfo] = exp.Message;
                    return View();
                }
        }
        #endregion
    }
}
